import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
// import config  from '../config/config';
import { ConfigService } from '@nestjs/config';
import { BadRequestException, ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';


async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const options = new DocumentBuilder()
  .setTitle('Cats example')
  .setDescription('The cats API description')
  .setVersion('1.0')
  .addTag('cats')
  .build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('doc', app, document);
  app.useGlobalPipes(
    new ValidationPipe({
      exceptionFactory(errors) {
          const firstError = errors[0]
          const message = firstError[Object.keys(firstError.constraints)[0]]
          return new BadRequestException(message)
      },
    })
  )
  const configService  = app.get(ConfigService);
  const port =  configService.get<number>('port')
  await app.listen(port);
}
bootstrap();
